package day03.io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @ClassName PWDemo2
 * @Description TODO
 * @Author xinghaiyang
 * @Date 2023/3/20 16:37
 * @Version 1.0
 * <p>
 * 练习流连接的操作,使用PrintWriter
 **/
public class PWDemo2 {
    public static void main(String[] args) throws FileNotFoundException {
        PrintWriter pw = new PrintWriter(
                new BufferedWriter(
                        new OutputStreamWriter(
                                new FileOutputStream("bw2.txt", true), StandardCharsets.UTF_8
                        )
                        /*流连接时才支持行刷新,所以第一个参数必须是个流,
                        如果时file或者path是不支持第二个参数为true自动行刷新的*/
                ),true//这里添加true后,自动调用flush,自动行刷新
        );
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入:");
            String line = sc.nextLine();
            if ("exit".equals(line)) {
                break;
            }
            pw.println(line);
            System.out.println("输出完毕");
        }
        pw.close();
    }
}
